Skip to content

fix(tui): avoid duplicate project initialization#22

Merged
CreatorGhost merged 2 commits into
devfrom
dedupe-project-init
Jul 12, 2026
Merged

fix(tui): avoid duplicate project initialization#22
CreatorGhost merged 2 commits into
devfrom
dedupe-project-init

Conversation

@CreatorGhost

@CreatorGhost CreatorGhost commented Jul 11, 2026

Copy link
Copy Markdown
Owner

Port of upstream anomalyco/opencode#35998.

Adds a process-local in-flight map with ownership tracking to the instance store so concurrent opens of the same directory share one bootstrap instead of racing duplicate initializations.

Verified: bun test test/project/instance.test.ts (10 pass, includes new concurrency regression tests) and bun typecheck from packages/opencode. Applied cleanly.

Summary by CodeRabbit

  • Bug Fixes

    • Improved reliability when multiple parts of the application load the same project concurrently.
    • Prevented duplicate initialization and ensured waiting operations finish correctly if initialization is interrupted or fails.
    • Improved instance cleanup so shared resources remain available until all active users release them.
    • Fixed reload and disposal behavior to avoid premature cleanup or stalled operations.
  • Tests

    • Added coverage for concurrent loading, shared-resource disposal, and interrupted initialization scenarios.

@coderabbitai

coderabbitai Bot commented Jul 11, 2026

Copy link
Copy Markdown

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 12a4bbc3-6afb-4920-a35f-f926e9c9bede

📥 Commits

Reviewing files that changed from the base of the PR and between 310b805 and d81bbe1.

📒 Files selected for processing (2)
  • packages/opencode/src/project/instance-store.ts
  • packages/opencode/test/project/instance.test.ts

📝 Walkthrough

Walkthrough

InstanceStore now coordinates initialization across independently built stores, tracks entry ownership, defers disposal until the final owner releases it, and propagates interrupted initialization failures. Tests cover deduplication, shared disposal, and interruption behavior.

Changes

Instance lifecycle coordination

Layer / File(s) Summary
Ownership and in-flight coordination
packages/opencode/src/project/instance-store.ts
Entries track owners, shared in-flight initialization is joined across stores, and deferred completion is settled on failure or interruption.
Load, reload, and disposal lifecycle
packages/opencode/src/project/instance-store.ts
Load, reload, and directory disposal use ownership release, conditional entry removal, and deferred exit validation.
Cross-store lifecycle tests
packages/opencode/test/project/instance.test.ts
Tests verify cross-store load deduplication, final-owner disposal, and failure completion after interrupting initialization.

Estimated code review effort: 4 (Complex) | ~45 minutes

Sequence Diagram(s)

sequenceDiagram
  participant StoreA
  participant StoreB
  participant InstanceStore
  participant Initializer
  participant Disposer

  StoreA->>InstanceStore: load directory
  InstanceStore->>Initializer: start initialization
  StoreB->>InstanceStore: load same directory
  InstanceStore-->>StoreB: join in-flight initialization
  Initializer-->>StoreA: resolve shared instance
  Initializer-->>StoreB: resolve shared instance
  StoreA->>InstanceStore: dispose owner
  InstanceStore-->>Disposer: retain instance while StoreB owns it
  StoreB->>InstanceStore: dispose final owner
  InstanceStore->>Disposer: dispose instance
Loading
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch dedupe-project-init

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint install failed. For unrecoverable errors, disable the tool in CodeRabbit configuration.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@CreatorGhost CreatorGhost force-pushed the dedupe-project-init branch from 8700272 to 9c51d8a Compare July 12, 2026 07:02
Addresses Opus review of PR #22: if an in-flight instance init fiber was
interrupted (owning layer scope closed mid-init), the shared entry.deferred was
never settled and cross-store joiners parked on it hung forever. completeLoad
now settles the deferred on every exit via Effect.ensuring. Deferred.interrupt
proved insufficient (it only woke the creating store's awaiter, not cross-store
joiners), so a concrete failure exit is used.

Adds two regression tests: refcounted cross-store disposal, and settling on
init interruption.
@CreatorGhost CreatorGhost force-pushed the dedupe-project-init branch from 9c51d8a to d81bbe1 Compare July 12, 2026 15:58
@CreatorGhost CreatorGhost merged commit 0bacf6b into dev Jul 12, 2026
3 of 6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant